import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";

/**
 *
 * @date 2018-11-23
 *
 * @notice 有数据转化
 */
import { getResponse, mapTree } from 'utils/utils';
import { queryIdpValue } from 'cpaas-front/lib/services/api';
import { fetchReceiveConfig as _fetchReceiveConfig, saveConfig as _saveConfig } from "../services/userReceiveConfigService";

function transformReceviConfig(dataSource) {
  if (dataSource) {
    return mapTree(dataSource, function (item) {
      var _item$defaultReceiveT = item.defaultReceiveTypeList,
          defaultReceiveTypeList = _item$defaultReceiveT === void 0 ? [] : _item$defaultReceiveT,
          _item$receiveTypeList = item.receiveTypeList,
          receiveTypeList = _item$receiveTypeList === void 0 ? [] : _item$receiveTypeList;
      var newReceiveTypeList = defaultReceiveTypeList.filter(function (tItem) {
        return !receiveTypeList.includes(tItem);
      });
      return _objectSpread(_objectSpread({}, item), {}, {
        receiveTypeList: newReceiveTypeList || [],
        receiveType: newReceiveTypeList.join(',')
      });
    }, {
      childrenNullable: true
    });
  }

  return dataSource;
}

export default {
  namespace: 'userReceiveConfig',
  state: {
    messageTypeList: [],
    // 所有的消息类型
    configList: [],
    // 消息配置列表
    addData: {},
    // 新增数据
    pathMap: {},
    // 节点层级路径
    configKeys: [] // 所有配置的的Id

  },
  effects: {
    // 获取消息类型
    fetchMessageType: /*#__PURE__*/_regeneratorRuntime.mark(function fetchMessageType(_, _ref) {
      var call, put, messageTypeList;
      return _regeneratorRuntime.wrap(function fetchMessageType$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.t0 = getResponse;
              _context.next = 4;
              return call(queryIdpValue, 'CMSG.MESSAGE_TYPE');

            case 4:
              _context.t1 = _context.sent;
              messageTypeList = (0, _context.t0)(_context.t1);

              if (!messageTypeList) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  messageTypeList: messageTypeList
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchMessageType);
    }),
    // 查询接收配置信息
    fetchReceiveConfig: /*#__PURE__*/_regeneratorRuntime.mark(function fetchReceiveConfig(_, _ref2) {
      var call, put, res, configList;
      return _regeneratorRuntime.wrap(function fetchReceiveConfig$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              call = _ref2.call, put = _ref2.put;
              _context2.next = 3;
              return call(_fetchReceiveConfig);

            case 3:
              res = _context2.sent;
              configList = transformReceviConfig(getResponse(res));

              if (!configList) {
                _context2.next = 8;
                break;
              }

              _context2.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  // configList: configList[0].children,
                  configList: configList
                }
              });

            case 8:
              return _context2.abrupt("return", configList);

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchReceiveConfig);
    }),
    // 保存下级配置
    saveConfig: /*#__PURE__*/_regeneratorRuntime.mark(function saveConfig(_ref3, _ref4) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function saveConfig$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context3.next = 4;
              return call(_saveConfig, payload);

            case 4:
              result = _context3.sent;
              return _context3.abrupt("return", transformReceviConfig(getResponse(result)));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, saveConfig);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};